<?php
session_start();
define('IN_ELS', true);
define('Z_PATH', str_replace('lib/apilogin/sinat/callback.php', '', str_replace('\\', '/', __FILE__)));

require_once(Z_PATH."lib/db.class.php");
include_once(Z_PATH.'lib/apilogin/sinat/config.php' );
include_once(Z_PATH.'lib/apilogin/sinat/weibooauth.php' );

$o = new WeiboOAuth( WB_AKEY , WB_SKEY , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret']  );
$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;
$_SESSION['last_key'] = $last_key;

$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] );
$me = $c->verify_credentials();
//$me['profile_image_url'] = str_replace('/50/','/180/',$me['profile_image_url']);

//$sContent = $c->follow('姑娘网');

$_SESSION['from_url'] = 'sina';
$sinat_user_id = "Sina#".$me['id'];
$user_id = check_sinat_user($sinat_user_id);


if ($user_id){	// 登陆
	login_user($user_id);
}else{			// 添加
	add_users($me,$sinat_user_id);
}


/* 检查是否有此用户 */
function check_sinat_user($sinat_id){
	define('ELS_DATADIR', Z_PATH.'data/');
	require ELS_DATADIR.'config.inc.php';
	$db = new db();	
	$db->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, UC_DBNAME, UC_DBCHARSET, UC_DBCONNECT, UC_DBTABLEPRE);
	
	$sql = " select id from gnw_user where ex_biaoshi='".$sinat_id."' ";
	
	$user_info = $db->fetch_first($sql);
	return $user_info['id'];
}
/*
 * 添加用户
*/
function add_users($me,$sinat_id){
	define('ELS_DATADIR', Z_PATH.'data/');
	define('ELS_ROOT', Z_PATH);
	require ELS_DATADIR.'config.inc.php';
	require_once(Z_PATH.'model/base.php');
	$dbs = new base();
	$now_time = time();
	$rands = rand(111111,999999);
	$data['user_name']   = "gnw_".$now_time.$rands."_sina";
	$data['password']    = md5("gnw_".$now_time.$rands."_sina");
	$data['nick_name']   = $me['name'];
	$data['user_type']   = 2;
	$data['ex_biaoshi']  = $sinat_id;
	$data['ex_source']   = "Sina";
	$data['last_ip']	 = $dbs -> getonlineip();
	$data['last_time']	 = $dbs -> getCurrTimeLine();
	$data['create_time'] = $dbs -> getCurrTimeLine();
	
	$retValue = $dbs->dbInsert("gnw_user",$data);
	$id = $dbs->db->insert_id();
	if($id){
		// 注册成功
		$_SESSION["mid"]			= $id;
		$_SESSION["nick_name"]		= $data['nick_name'];
	}
	$qqbackurl = "http://local.iknowy.com/";
	header ("Location: ".$qqbackurl);
	exit;
}

/*
 * 登陆用户
*/
function login_user($user_id){
	define('ELS_DATADIR', Z_PATH.'data/');
	define('ELS_ROOT', Z_PATH);
	require ELS_DATADIR.'config.inc.php';
	require_once(Z_PATH.'model/base.php');
	$dbs = new base();	
	
	$sql = " select id,nick_name,true_name from gnw_user where id='".$user_id."' ";
	
	$user_info = $dbs->db->fetch_first($sql);
	if($user_info){
		$dbs->db->query("UPDATE gnw_user SET last_ip = '".$dbs -> getonlineip()."',last_time='".$dbs -> getCurrTimeLine()."' WHERE id='$user_id'");
		
		// 注册成功
		$_SESSION["mid"]			= $user_info['id'];
		$_SESSION["true_name"]		= $user_info['true_name'];
		$_SESSION["nick_name"]		= $user_info['nick_name'];
	}
	$qqbackurl ="http://local.iknowy.com/";
	header ("Location: ".$qqbackurl);
	exit;
}

?>